﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace LibraryManagement
{
    public partial class DeleteBookForm : Form
    {
        private Form parentForm;
        //private string adminId;
        public DeleteBookForm(Form parent)
        {
            InitializeComponent();
            parentForm = parent;
            //this.adminId = adminId;
            LoadBooks();
        }
        private void LoadBooks()
        {
            string query = "SELECT BookID, Title, Author, ISBN,Status FROM Books";
            DataTable dt = DatabaseHelper.ExecuteQuery(query);
            dgvBooks.DataSource = dt;
        }

        private void DeleteBookForm_Load(object sender, EventArgs e)
        {
            LoadBooks();
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            parentForm.Show();
            this.Close();
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {
            if (dgvBooks.SelectedRows.Count > 0)
            {
                int bookId = (int)dgvBooks.SelectedRows[0].Cells["BookID"].Value;
                string deleteQuery = $"update Books set Status='下架' WHERE BookID = {bookId}";
                DatabaseHelper.ExecuteNonQuery(deleteQuery);

                // 记录日志
                string logQuery = $"INSERT INTO Logs (Action, BookID, Operator, Date) VALUES ('DropBook', {bookId}, '1', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
                DatabaseHelper.ExecuteNonQuery(logQuery);

                MessageBox.Show("图书下架成功！");
                LoadBooks();
            }
            else
            {
                MessageBox.Show("请选择一本图书！");
            }
        }
    }
}
